// 更新数据库题目代码 修改读取的两个文件名称，和保存的文件名称，运行该文件，就可以把题目保存到数据库

const fs = require('fs')
const Wxquestion = require('../../models/Wxquestion')
const path = require('path')
require('../../plugins/db.js')()

console.log(123)

function upload(){
    fs.readFile(path.resolve(__dirname, './xiaochekeyi.json'), function (err, data) {
        if (err) {
            return console.error(err);
        }
        let questions = data.toString(); //将二进制的数据转换为字符串
        questions = JSON.parse(questions); //将字符串转换为json对象
        const keyi = questions.data
        keyi.map((item, index) => {
            item.questionId = index
            item.shoucang = 'shoucang-copy-copy'
            item.userdxanswer = []
            if(item.answer == '对' || item.answer == '错'){
                return item.questiontype = '判断题'
            }
            if(item.answer.length == 1){
                return item.questiontype = '单选题'
            }
            return item.questiontype = '多选题'
        })

        fs.readFile(path.resolve(__dirname, './xiaochekesi.json'), function (err, data){  
            if(err){ return console.error(err)}
            let test = data.toString()
            test = JSON.parse(test)
            const kesi = test.data
            kesi.map((item, index) => {
                item.questionId = index
                item.shoucang = 'shoucang-copy-copy'
                item.userdxanswer = []
                if(item.answer == '对' || item.answer == '错'){
                    return item.questiontype = '判断题'
                }
                if(item.answer.length == 1){
                    return item.questiontype = '单选题'
                }
                return item.questiontype = '多选题'
            })

            Wxquestion.create({
                chexing: 'xiaoche',
                keyi: keyi,
                kesi: kesi
            }, (err, data)=> {console.log('更新完成')})
        })
    })
}
upload()